//
//  KoiAccountingNetApi.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 19.03.25.
//

import Foundation
import Alamofire

class KoiAccountingNetApi : KoiNetBase {
    
    static let standard = KoiAccountingNetApi()
    
    func testNet(result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        request(
            path: "testnet",
            method: .get,
            result: result
        )
    }
    
    func loginTrial(result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestLogin(
            path: "account/login/trial",
            method: .post,
            result: result
        )
    }
    
    func loginThird(loginParams:[String: String], result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestLogin(
            path: "account/login/auth/third",
            method: .post,
            parameters: loginParams,
            result: result
        )
    }
    
    func loginPassword(phone: String, password: String, result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestLogin(
            path: "account/login/auth/password",
            method: .post,
            parameters: ["account": phone, "password": password],
            result: result
        )
    }
    
    func sendCaptcha(phone: String, result: @escaping (Result<CaptchaData?, KoiNetError>) -> Void) {
        requestLogin(
            path: "account/login/phone/captcha/send",
            method: .post,
            parameters: ["phone": phone],
            result: result
        )
    }
    
    func loginCaptchaSubmit(phone: String, captcha: String, result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestLogin(
            path: "account/login/phone/captcha/submit",
            method: .post,
            parameters: ["phone": phone, "captcha": captcha],
            result: result
        )
    }
    
    func passwordSet(password: String, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/password/set",
            method: .post,
            parameters: ["password": password],
            result: result
        )
    }
    
    func passwordModify(oldPassword: String, newPassword: String, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/password/modify",
            method: .post,
            parameters: ["old_password": oldPassword, "new_password": newPassword],
            result: result
        )
    }
    
    func phoneBind(phone: String, captcha: String, result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/phone/bind",
            method: .post,
            parameters: ["phone": phone, "captcha": captcha],
            result: result
        )
    }
    
    func thirdBind(loginParams:[String: String], result: @escaping (Result<KoiWxProfile?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/third/bind",
            method: .post,
            parameters: loginParams,
            result: result
        )
    }
    
    func logout(result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        let refreshToken = KoiTokenStore.getRefreshToken()
        requestAuth(
            path: "account/logout",
            method: .post,
            headers: ["rt": refreshToken],
            result: result
        )
    }
    
    func remove(result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        let refreshToken = KoiTokenStore.getRefreshToken()
        requestAuth(
            path: "account/remove",
            method: .post,
            headers: ["rt": refreshToken],
            result: result
        )
    }
    
    func nicknameModify(nickname: String, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/nickname/modify",
            method: .post,
            parameters: ["nickname": nickname],
            result: result
        )
    }
    
    func ageModify(age: String, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/age/modify",
            method: .post,
            parameters: ["age": age],
            result: result
        )
    }
    
    func sexModify(sex: Int, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/sex/modify",
            method: .post,
            parameters: ["sex": sex],
            result: result
        )
    }
    
    func userDataInit(is_init: String, result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/user_data_init",
            method: .post,
            parameters: ["is_init": is_init],
            result: result
        )
    }
    
    func userProfile( result: @escaping (Result<KoiAccountingUser?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/userprofile",
            method: .get,
            result: result
        )
    }
    
    func refreshToken(result: @escaping (Result<KoiToken?, AFError>) -> Void) {
        let rt = KoiTokenStore.getRefreshToken()
        AF.request(KoiNetBase.HOST + "account/token/refresh", method: .get, headers: ["rt":rt])
            .validate(statusCode: 200..<300) // 提前验证状态码，否则会先报解析错误，无法知道状态码是多少
            .responseDecodable { (response:AFDataResponse<KoiNetResponse<KoiToken>>) in
            switch response.result {
            case .success(let koiResponse):
                result(Result.success(koiResponse.data))
            case .failure(let error):
                result(Result.failure(error))
            }
        }
    }
    
    func updateNeedInit(result: @escaping (Result<KoiData?, KoiNetError>) -> Void) {
        requestAuth(
            path: "account/user_data_init",
            method: .post,
            parameters: ["is_init" : 1],
            result: result
        )
    }
}
